<?php
namespace Home\Model;
use Think\Model;
class ArticleModel extends Model {
	public $table='think_article';
	public $page; //当前页
	public $perpage; //每页显示
	public $article_id; //ID
	public $ac_id; //分类ID
	public $post; //提交的信息 数组
	public $ids; //IDs 主要用于删除
	public $sorts; //IDs 主要用于删除
	public $keyword; //关键词
	public $system_cat_list; //关键词
	public $ac_ids;
	public $limit_num;  //限制数
	public $url;
	public  $pic_page;
	public  $banner = array();
	//获取信息
	public function get_article_info()
	{
		$article_id=intval($this->article_id);
		$Model=M("Article");
		$ArticleClass=M("Article_class");
		$info=$Model->where('article_id='.$article_id)->find();
		$info['ac_name']=$ArticleClass->where('ac_id='.$info['ac_id'])->getField('ac_name');
		//print_r($info);exit;
		return $info;
	}
	//获得上一篇文章信息
	public function get_last_article()
	{
		$article_id=intval($this->article_id);
		$ac_id=intval($this->ac_id);
		$Model=new Model();
		$query=$Model->query("SELECT article_id,article_title from ".$this->table." WHERE article_id<$article_id AND `ac_id`=$ac_id ORDER by `article_id` desc limit 1");
		return $query[0];
	}

	//获得下一篇文章信息
	public function get_next_article()
	{
		$article_id=intval($this->article_id);
		$ac_id=intval($this->ac_id);
		$Model=new Model();
		$query=$Model->query("SELECT article_id,article_title from ".$this->table." WHERE article_id>$article_id AND `ac_id`=$ac_id ORDER by `article_id` asc limit 1");
		return $query[0];
	}

	//获得文章列表 $page当前页 $perpage每页显示
	public function get_article_list()
	{
		$page=$this->page;
		$perpage=$this->perpage;
		//搜索
		$where='where article_type=1 ';
		/*if($this->keyword){
			$where.="and `article_title` LIKE '%".$this->keyword."%'";
		}*/
		if($this->ac_id){
			$where.="and c.ac_id=".$this->ac_id;
		}
		$start=($page-1)*$perpage;
		$Model=new Model();
		$list=$Model->query("select * from `think_article` a left join `think_article_class` c on a.ac_id=c.ac_id ".$where." order by a.article_sort asc,a.article_time desc limit $start,$perpage");
		//dump($list);exit;

		foreach($list as $k=>$v){
			$url="";
			preg_match('/<img.*?src="(.*?)".*?>/is',$v["article_content"],$array);
			if($array[1]){
				$url_array = explode("/",$array[1]);
				foreach($url_array as $key=>$v){
					if($key>2){
						$url.="/". $v;
					}
				}

			}
			$list[$k]['cover_url']=$url;
		}


		return $list;
	}
	//获得文章总数
	public function get_article_total()
	{
		//搜索
		$where='where article_type=1 ';
		/*if($this->keyword){
			$where.="and `article_title` LIKE '%".$this->keyword."%'";
		}*/
		if($this->ac_id){
			$where.="and ac_id=".$this->ac_id;
		}
		$Model=new Model();
		$query=$Model->query("select count(article_id) as total from `think_article` ".$where." ");
		return $query[0]['total'];
	}
	//获得首页文章列表 $limit_num 限制数 $ac_id分类ID
	public function get_index_article_list()
	{
		$limit_num=$this->limit_num;
		$ac_id=$this->ac_id;
		$field='article_id,article_title,article_time,article_origin';
		$where='article_type=1';
		$limit='0,10';
		//where
		if($ac_id>0){
			$where.=" and ac_id=".$ac_id;
		}
		//limit
		if($limit_num>0){
			$limit="0,".$limit_num;
		}
		$Model=M('Article');
		$list=$Model->field($field)->where($where)->order('article_sort asc,article_id desc')->limit($limit)->select();
		foreach($list as $k=>$v){
			$article_content=$Model->where('article_id='.$v['article_id'])->getField('article_content');
			$list[$k]['article_content']=strip_tags($article_content);
			$url="";

			preg_match('/<img.*?src="(.*?)".*?>/is',$article_content,$array);
			if($array[1]){
				$url_array = explode("/",$array[1]);
				foreach($url_array as $key=>$v){
					if($key>2){
						$url.="/". $v;
					}
				}
				$list[$k]['cover_url']=$url;
			}

		}
		return $list;
	}

	public  function get_banner_article_list(){
		$this->banner = array();
		return $this->get_banner_article(1);
	}
	private  function get_banner_article($page)
	{
		$ac_id=$this->ac_id;
		//$banner_num=$this->num;
		$field='article_id,article_title,article_time,article_origin';
		$where='article_type=1';

		//where
		if($ac_id>0){
			$where.=" and ac_id=".$ac_id;
		}
		$Model=M('Article');
		$list=$Model->field($field)->where($where)->order('article_sort asc,article_id desc')->limit(($page-1)*50,$page*50)->select();

		foreach($list as $k=>$v){
			$article_content=$Model->where('article_id='.$v['article_id'])->getField('article_content');
			$list[$k]['article_content']=strip_tags($article_content);
			$url="";
			preg_match('/<img.*?src="(.*?)".*?>/is',$article_content,$array);
			if($array[1] && $this->num>0){
				$url_array = explode("/",$array[1]);
				foreach($url_array as $key=>$v){
					if($key>2){
						$url.="/". $v;
					}
				}
				$list[$k]['cover_url']=$url;
				$this->num--;
				$this->banner[]=$list[$k];
			}
		}
		if(count($this->banner)<$this->num){
			$this->pic_page++;
			$this->get_banner_article($this->pic_page);
		}
		return $this->banner;
	}


	public function get_special_news_list()
	{
		$ac_id=33;
		$banner_num=3;
		$field='article_id,article_title,article_time,article_origin';
		$where='article_type=1';
		$limit='0,50';

		if($ac_id>0){
			$where.=" and ac_id=".$ac_id;
		}
		$Model=M('Article');
		$list=$Model->field($field)->where($where)->order('article_sort asc,article_id desc')->limit($limit)->select();

		foreach($list as $k=>$v){
			$list[$k]["article_content"]=$Model->where('article_id='.$v['article_id'])->getField('article_content');
			preg_match('/<img.*?src="(.*?)".*?>/is',$list[$k]["article_content"],$array);
			$list[$k]['article_content']=strip_tags($list[$k]['article_content']);
			if(!$array[1] && $banner_num<15){
				$list_word[]=$list[$k];
			}
		}

		foreach($list as $k=>$v){
			$article_content=$Model->where('article_id='.$v['article_id'])->getField('article_content');
			$url="";
			preg_match('/<img.*?src="(.*?)".*?>/is',$article_content,$array);
			if($array[1] && $banner_num>0){
				$url_array = explode("/",$array[1]);
				foreach($url_array as $key=>$v){
					if($key>2){
						$url.="/". $v;
					}
				}
				$list[$k]['cover_url']=$url;
				$banner_num--;
				$list_pic[]=$list[$k];
			}
		}

		for($i=0;$i<3;$i++) {
			$lists[$i]["list_pic"]=$list_pic[$i];
			$lists[$i]["list_word"]=array_slice($list_word,$i*5,5);
		}

		return $lists;
	}

	//获取所有的投票记录
	public function get_allvote_list($article_type){
		$Model = new Model();
		$query = $Model->query('select v.article_id,count(*) as vote_num,a.article_title,a.article_thumb from think_vote v left join think_article a on a.article_id=v.article_id where v.article_type='.$article_type.' group by v.article_id order by count(*) desc limit 0,3');
		return $query;
	}

	public function get_allvote($article_type){
		$Model = new Model();
		$query = $Model->query('select v.article_id,count(*) as vote_num,a.article_title,a.article_thumb from think_vote v left join think_article a on a.article_id=v.article_id where v.article_type='.$article_type.' group by v.article_id order by count(*) desc');
		return $query;
	}
}